package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.ExtraFeeForm;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.security.AuthUser;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.SysVarDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.ExtraFeeBiz;
import guda.house.dal.dao.ExtraFeeDAO;
import guda.house.dal.dataobject.ExtraFeeDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class ExtraFeeBizImpl implements ExtraFeeBiz{

    private final static Logger logger = LoggerFactory.getLogger(ExtraFeeBizImpl.class);

    @Autowired
    private ExtraFeeDAO extraFeeDAO;
    @Autowired
    private SysVarDAO sysVarDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ExtraFeeDO extraFeeDO = extraFeeDAO.getExtraFeeById(id);
            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(extraFeeDO.getVillageId());
            sysVarQuery.setVarType(SysVarTypeEnum.extra_fee.name());
            List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
            bizResult.data.put("feeTypeSysVarList",sysVarDOs);
            bizResult.data.put("extraFeeDO", extraFeeDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query ExtraFee error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = extraFeeDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<ExtraFeeDO> extraFeeList = extraFeeDAO.selectForPage(baseQuery);
            bizResult.data.put("extraFeeList", extraFeeList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view ExtraFee list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            extraFeeDAO.delExtraFeeById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete extraFee error", e);
        }
        return bizResult;
    }

    public BizResult create(ExtraFeeForm extraFeeForm) {
        BizResult bizResult = new BizResult();
        try {
            ExtraFeeDO extraFeeDO = extraFeeForm.toDO();
            SysVarDO sysVarById = sysVarDAO.getSysVarById(extraFeeForm.getVarId());
            extraFeeDO.setFeeType(sysVarById.getVarValue());
            extraFeeDO.setAdminId(AuthUser.getCurrentUser().getUserId());
            long id = extraFeeDAO.insert(extraFeeDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create ExtraFee error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(ExtraFeeDO extraFeeDO) {
        BizResult bizResult = new BizResult();
        try {
            SysVarDO sysVarById = sysVarDAO.getSysVarById(extraFeeDO.getVarId());
            extraFeeDO.setFeeType(sysVarById.getVarValue());
            extraFeeDAO.updateByIdSelective(extraFeeDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update ExtraFee error", e);
        }
        return bizResult;
    }


}
